package com.wolf.gulimall.cart.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession;
import org.springframework.session.web.http.CookieSerializer;
import org.springframework.session.web.http.DefaultCookieSerializer;

/**
 * @Description:    springSession配置类
 * @Created: with IntelliJ IDEA.
 * @author: wolfmoon_hu
 * @createTime: 2022-11-27
 **/
@EnableRedisHttpSession //开启redisSession
@Configuration
public class GuLiMallSessionConfig {
    /**
     *  springSession核心原理
     * 1)、@EnableRedisHttpSession导入RedisHttpSessionConfiguration配置
     *     1、给容器中添加了一个组件SessionRepository=》》》[RedisOperationsSessionRepository]==》Redis操作session，session的增删改查封装类
     *     2、SessionRepositoryFiter==》Filter: session'存储过滤器;每个请求过来都必须经过filter
     *       1、创建的时候，就自动从容器中获取到了sessionRepository;
     *       2、原始的request，response都被包装。SessionRepositoryRequestWrapper, SessionRepositoryResponseh
     *       3、以后获取session。request.getSession();
     *       4、wrappedRequest.getSession();===> SessionRepository 中获取到的。
     *   利用了装饰着模式
     */

    @Bean
    public CookieSerializer cookieSerializer() {

        DefaultCookieSerializer cookieSerializer = new DefaultCookieSerializer();

        //放大作用域
        cookieSerializer.setDomainName("gulimallwolf.com");
        cookieSerializer.setCookieName("GULISESSION");

        return cookieSerializer;
    }


    @Bean
    public RedisSerializer<Object> springSessionDefaultRedisSerializer() {
        return new GenericJackson2JsonRedisSerializer();
    }

}
